<?PHP

class OP_FeatureCarouselWidget extends OP_WidgetTemplate{		
	function render(){
		$extratag = strlen($this->values['tag']) > 0 ? ' '.$this->values['tag'] : '';
		$title = $this->values['title'];
		
		if(is_admin()) return;
		
		$features = get_posts('tag=feature'.$extratag.'&orderby=date&numberposts=-1');
		
		/**
		 *  Build cases where we want to filter out posts to show in widget.
		 * 
		 * 	Do this outside the following render loop..
		 * 
		 */
		global $post;
		global $OnePress;

		$features = $OnePress->feature_registry->get_available($features);

		if(count($features) > 0){
			$content = array();
			for($i = 0; $i < min(4,count($features)); $i++){
				$link = get_permalink($features[$i]->ID);
			
				if($features[$i]->post_excerpt){
					$post = $features[$i]->post_excerpt;
				}
				else{
					$post = $features[$i]->post_content;
					$post = strlen($post) > 300 ? substr(strip_tags($post),0,300).'...' : strip_tags($post);
				}
			
				$img = OP_ImageSelection::get_image($features[$i]->ID,'feature');
				
				$content[] = array(
					'title'		=> $features[$i]->post_title,
					'link' 		=> $link,
					'image'		=> $img,
					'excerpt'	=> $post,
					'all'		=> $features[$i]
				);
			}
		}
		
		include(TEMPLATEPATH.'/widgets/template.feature_carousel.php');
	}
	
	function do_post($post){
		$title = strip_tags(stripslashes($post['title']));
		$tag = strip_tags(stripslashes($post['tag']));
		$transition = strip_tags(stripslashes($post['transition']));
		$style = strlen($post['style']) == 0 ? 'default' : $post['style']; // this widget has a default style
		return compact( 'title','tag','transition','style' );
	}
	
	function options(){
		extract($this->values);
		$title = attribute_escape($title);
		$tagextra = attribute_escape($tag);
	
		$alltags = get_tags('orderby=name&order=ASC');
		
		$alltagnames = array();
		foreach($alltags as $tag) {
			if($tag->slug == 'feature') continue;
			$alltagnames[$tag->slug] = array('name'=>$tag->name,'slug'=>$tag->slug);
		}
		
		$tagtxt = strlen($tagextra) > 0 ? 'and "'.$alltagnames[$tagextra]['name'].'"' : '';
		echo 'Tag <a href="/wp-admin/edit.php">Posts</a> "feature"'.$tagtxt.' for them to appear in this widget!',
			'<input class="widefat" type="text" id="feature_carousel-title-'.$number.'" name="widget_op_feature_carousel['.$number.'][title]" value="'.wp_specialchars($title, true).'" />',
			'<p>';
		
		if(count($alltagnames)>0){
			echo '<label>'. __('Select tag').'</label><select name="widget_op_feature_carousel['.$number.'][tag]">';
			echo '<option></option>';
			foreach($alltagnames as $tag){
				$checked = $tagextra == $tag['slug'] ? 'SELECTED' : '';
				echo '<option value="'.$tag['slug'].'" '.$checked.'>'.$tag['name'].'</option>';	
			}
			echo '</select>';
		}
	
		echo '</p>';
		
		echo '<label>'. __('Transition Effect').'</label><select name="widget_op_feature_carousel['.$number.'][transition]">';
		echo '<option></option>';
		$transitions = array('verticalscroll','crossfade');
		foreach($transitions as $transition){
			$checked = $this->values['transition'] == $transition ? 'SELECTED' : '';
			echo '<option value="'.$transition.'" '.$checked.'>'.$transition.'</option>';
		}
		echo '</select>';
		
		if ( function_exists('wp_nonce_field') )
			wp_nonce_field('feature_carousel_remove_tag','_opremovetag');

	}

	// function remove_tag(){
	// 	if(check_ajax_referer('feature_carousel_remove_tag','nonce') && current_user_can('manage_options')){
	// 		$post_id = attribute_escape($_POST['post_id']);
	// 		$tags = wp_get_post_tags($post_id, array('fields'=>'names'));
	// 		$tags = array_diff($tags, array('feature'));
	// 		wp_set_post_tags($post_id, implode(',',$tags));
	// 	}
	// }
	
	function head(){
		$url = get_bloginfo('template_directory').'/';
		wp_register_script('opfeaturecarouseljs', $url.'scripts/feature_carousel.js',array('onepressjs'));
		wp_enqueue_script('opfeaturecarouseljs');

	}
	
	function admin_head(){
		$url = get_bloginfo('template_directory').'/op/admin/';
	}
}

$widget = new OP_Widget('feature_carousel');
$widget->setMeta('OP Featured Posts Carousel','Show featured posts in carousel');
$widget->setDefaults(array('title'=>'','transition'=>'crossfade','style'=>'default'));

$opw_feature_carousel = new OP_FeatureCarouselWidget($widget);
add_action('widgets_init', array($opw_feature_carousel, 'init'));
?>